package com.gzsz.service;

import com.gzsz.util.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static java.lang.System.out;

public class PatientServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException{
        DBUtil dbUtil=new DBUtil();
        out.println("entry -----");
        String method= request.getParameter("method");
        if (method.equals("delete")){
            String patient_id= request.getParameter("patient_id");
            String delete_sql="delete from patient where patient_id="+patient_id+";";
            if (dbUtil.executeUpdateBySQL(delete_sql)>0){
                resp.sendRedirect("html1/patient.jsp");
            }else {
                out.println("删除失败");
            }
        }
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException{
        String method= request.getParameter("method");
        DBUtil dbUtil=new DBUtil();
        if (method.equals("add")){
            //1.获取表单的值
            String patient_name = request.getParameter("patient_name");
            String gender = request.getParameter("gender");
            String age = request.getParameter("age");
            String birth_date = request.getParameter("birth_date");
            String id_number = request.getParameter("id_number");
            String contact_number = request.getParameter("contact_number");
            String ward_id = request.getParameter("ward_id");
            String bed_id = request.getParameter("bed_id");
            String doctor_id = request.getParameter("doctor_id");
            String diagnosis = request.getParameter("diagnosis");
            String admission_date = request.getParameter("admission_date");
            String expected_discharge_date = request.getParameter("expected_discharge_date");
            String actual_discharge_date = request.getParameter("actual_discharge_date");
            //2.拼接SQL语句执行数据库
            String add_sql="INSERT INTO patient (patient_name, gender, age, birth_date, id_number, contact_number, ward_id, bed_id,doctor_id, diagnosis, admission_date, expected_discharge_date,actual_discharge_date) " +
                    "VALUES ('"+patient_name+"', '"+gender+"', '"+age+"', '"+birth_date+"', '"+id_number+"', '"+contact_number+"'," +
                    " '"+ward_id+"', '"+bed_id+"', '"+doctor_id+"', '"+diagnosis+"', '"+admission_date+"', '"+expected_discharge_date+"', '"+actual_discharge_date+"');";
            if (dbUtil.executeUpdateBySQL(add_sql)>0){
                resp.sendRedirect("html1/patient.jsp");
            }else {
                out.println("添加失败");
            }
        } else if (method.equals("edit")) {

            //1.获取表单的值
            String patient_id = request.getParameter("patient_id");
            String patient_name = request.getParameter("patient_name");
            String gender = request.getParameter("gender");
            String age = request.getParameter("age");
            String birth_date = request.getParameter("birth_date");
            String id_number = request.getParameter("id_number");
            String contact_number = request.getParameter("contact_number");
            String ward_id = request.getParameter("ward_id");
            String bed_id = request.getParameter("bed_id");
            String doctor_id = request.getParameter("doctor_id");
            String diagnosis = request.getParameter("diagnosis");
            String admission_date = request.getParameter("admission_date");
            String expected_discharge_date = request.getParameter("expected_discharge_date");
            String actual_discharge_date = request.getParameter("actual_discharge_date");
            //2.拼接SQL语句执行数据库
            String edit_sql="UPDATE patient SET patient_id='"+patient_id+"',patient_name='"+patient_name+"'," +
                    "gender='"+gender+"',age='"+age+"',birth_date='"+birth_date+"',id_number='"+id_number+"'," +
                    "contact_number='"+contact_number+"',ward_id='"+ward_id+"',bed_id='"+bed_id+"',doctor_id='"+doctor_id+"'," +
                    "diagnosis='"+diagnosis+"',admission_date='"+admission_date+"',expected_discharge_date='"+expected_discharge_date+"'," +
                    "actual_discharge_date='"+actual_discharge_date+"' where patient_id='"+patient_id+"'";

            if (dbUtil.executeUpdateBySQL(edit_sql)>0){
//                System.out.println("编辑成功");
                resp.sendRedirect("html1/patient.jsp");
            }else {
                out.println("编辑失败");
            }
        }
    }
}




